package com.baoming.web.action;
 

import org.apache.log4j.Logger; 

import com.baoming.container.ServiceContainer;
import com.baoming.domain.Member;
import com.baoming.domain.Site;
import com.baoming.service.IMemberService;
import com.baoming.service.ISiteService;
import com.baoming.tool.LogerTool; 
import com.baoming.util.LoginUtil;
import com.baoming.util.ServiceTool;
import com.opensymphony.xwork2.ModelDriven;

@SuppressWarnings("serial")
public class LoginAction extends BaseAction implements ModelDriven<Member>{
	
	private IMemberService memberService = (IMemberService) ServiceContainer.getService(IMemberService.SERVICE) ; 
	private ISiteService siteService = (ISiteService) ServiceContainer.getService(ISiteService.SERVICE) ; 
	private Member member = new Member() ; 
	 
	private Logger logger = Logger.getLogger(LoginAction.class.getName());
	   
	/**
	 * 进入注册界面 
	 * @return
	 */
	public String enterRegist(){
		 
		LogerTool.info(logger, request, "进入注册页面") ; 
		
		return "enterRegist" ; 
	}
	 
	/**
	 * 进入主面板
	 * @return
	 */
	public String main(){
		LogerTool.info(logger, request, "进入管理面版") ;  
		return "main" ; 
	}
	
	/**
	 * 退出登陆
	 * @return
	 */
	public String exit(){
		//更新用户退出时间
		Member member = (Member) request.getSession().getAttribute("global_member") ;  
		
		//更新用户退出时间
		memberService.updateMemberLoginTime(ServiceTool.getNowDate() , member.getId()) ; 
		
		LogerTool.info(logger, request, "退出登陆") ; 
		
		//消除所有Session
		request.getSession().invalidate() ; 
		return "exit" ; 
	}
	
	/**
	 * 进入页面
	 */
	public String enter(){
		/*
		 * 验证登陆用户
		 */
		LoginUtil.checkCookies(request) ; 
		System.out.println("Im Ok!");
		return LOGIN ; 
	}
	
	/**
	 * 验证登陆 
	 */
	public String checkLogin(){
		 
		//查询出用户
		Member gmember = memberService.checkLoginMember(member.getEmail().trim() , member.getPassword().trim()) ; 
		  
		if(gmember != null){
			
			//查询用户是否已经审核 
			if(gmember.getIsAllow().equals("no")){
				//没有审核的错误码为2
				request.getSession().setAttribute("error", "2") ; 
				LogerTool.info(logger, request, "登陆 ，用户没有审核 ，登陆失败") ;  
				return "checkLoginError"  ; 
			}
			
			//查询出用户的站点信息
			Site site = siteService.findSiteByMember(gmember.getId()) ; 
			if(site != null){
				gmember.setSite(site) ; 
			}
			
			gmember.setLoginCount(gmember.getLoginCount()+1) ; 
			memberService.updateMember(gmember) ; 
			request.getSession().setAttribute("global_member", gmember) ; 
			
			//登陆成功 消除错误信息
			if(request.getSession().getAttribute("error")==null){
				request.getSession().removeAttribute("error") ; 
			}
			LogerTool.info(logger, request, "登陆成功") ;   
			return "checkLogin"  ; 
		}
		LogerTool.info(logger, request, "尝试登陆失败  ，不存在这个用户") ;    
		//登陆验证失败 返回失败代码 1 失败信息设置为Session属性 为重定向传值
		request.getSession().setAttribute("error", "1") ; 
		
		return "checkLoginError"  ; 
	}
	
	/**
	 * 用户注册
	 * @return
	 */
	public String regist(){
		 
		String code = request.getParameter("code").trim();
		
		if (LoginUtil.checkNumber(request, code)) {
			/*
			 * 查询用户是否存在
			 */
			IMemberService memberService = (IMemberService) ServiceContainer
					.getService(IMemberService.SERVICE);
			if (memberService.isHas(member.getEmail())) {
				request.setAttribute("regist_tip", "has");
				LogerTool.info(logger, request, request.getRemoteHost() + " 注册不成功  ， 用户名为 " + member.getName() + " , 邮箱为 " + member.getEmail() + " , 用户已经存在  " + ServiceTool.getNowDate()) ;
				return "registError";
			}
		}else{
			LogerTool.info(logger, request, request.getRemoteHost() + " 注册不成功  ， 用户名为 " + member.getName() + " , 邮箱为 " + member.getEmail() + " , 验证码不正确   " + ServiceTool.getNowDate()) ;
			//验证码不正确
			request.setAttribute("regist_tip", "code");
			return "registError";
		}
		
		
		LogerTool.info(logger, request, request.getRemoteHost() + " 注册成功  ， 用户名为 " + member.getName() + " , 邮箱为 " + member.getEmail() + " " + ServiceTool.getNowDate()) ;
		
		member.setRegistIp(request.getRemoteAddr()) ; 
		member.setRegistTime(ServiceTool.getNowDate()) ; 
		member.setIsDelete(1) ; 
		member.setIsAllow("yes") ;  
		
		memberService.saveMember(member) ; 
		  
		return "registTip" ;
	}
	
	/**
	 * 找回密码
	 */
	public String findPass(){
		
		String email = member.getEmail() ; 
		
		System.out.println("email = " + email);
		
		return SUCCESS ; 
	}

	public Member getModel() {
		return member ;
	}

	@Override
	public String add() {
		return null;
	}

	@Override
	public String edit() {
		return null;
	}

	@Override
	public String manager() {
		return null;
	}

	@Override
	public String remove() {
		return null;
	}

	@Override
	public String removeGroup() {
		return null;
	}
	
}
